<?php
/**
 * @author GhostWei
 * @date Sep 29, 2012
 */
class controller_admin_index_ajax extends system_user_controller_ajax {
	
	public function init() {
		if(!parent::init()) {
			$this->_error('Login time out or other reason.');
		}
	}
		
	public function action_translate() {		
		if(!($api = $this->_TApi($this->_post('api')))) {
			$this->_error("API is not existing");
		}
		$result = $api->setFrom($this->_post('f'))->setTo($this->_post('t'))->setText($this->_post('text'))->translate();
		if(1 && $err = $result->getError()) {
			$this->_error($err);
		}		
		$this->_data->text = $result->getResult();
		$this->_output();
	}
	
	/**
	 * @return translator_abstract
	 */
	private function _TApi($api) {
		try {
			$class = 'translator_api_' . strtolower($api);
			return new $class();
		} catch (Exception $e) {
			return null;
		}
	}
	
	
}
